/**
 * 仓库中心
 */
import { Application } from "egg";
import { ObjectId } from "mongoose";
import * as AutoIncrementFactory from "mongoose-sequence";
export interface WarehouseProps {
  company_id: string; // 所属公司
  drug: ObjectId; // 药品名称【入库】
  price: number; // 药品销售价（前端是必填的）
  entryPrice: number; // 药品进价（前端是必填的）
  brand: ObjectId; // 药品品牌【入库】（前端是必填的）
  standard: ObjectId; // 药品规格【入库】（前端是必填的）
  productionDate: Date; // 生产日期【入库】
  expirationDate: Date; // 过期日期【入库】
  factory: ObjectId; // 生产厂商【入库】（前端是必填的）
  business: ObjectId; // 所属商业公司名称【各个分仓库】【入库】
  amount: number; // 当前库存【入库】
  batches: string; // 入库批号【入库】
  flow: []; // 药品流向【入库】
  is_delete: boolean; // 是否删除
}
export interface WarehouseDrugProps {
  _id: ObjectId;
  name: string; // 药品名称
  brand: string;
  standard: string;
  price: number;
  entryPrice: number; // 药品进价
  factory: string;
}
export interface WarehouseModelProps {
  _id: ObjectId;
  company_id: ObjectId; // 所属公司
  drug: WarehouseDrugProps; // 药品名称【入库】
  productionDate: Date; // 生产日期【入库】
  expirationDate: Date; // 过期日期【入库】
  business: string; // 所属商业公司名称【各个分仓库】【入库】
  amount: number; // 当前库存【入库】
  batches: string; // 入库批号【入库】
  flow: Array<string>; // 药品流向【入库】
  is_delete: boolean; // 是否删除
  createdAt: number;
  updatedAt: number;
}
function initWarehouseModel(app: Application) {
  const mongoose = app.mongoose;
  const Schema = mongoose.Schema;
  const AutoIncrement = AutoIncrementFactory(mongoose);
  const WarehouseScheme = new Schema<WarehouseModelProps>(
    {
      company_id: {
        type: Schema.Types.ObjectId,
        ref: "Company",
        required: true,
      },
      drug: { type: Object, required: true },
      productionDate: { type: Date, required: true },
      expirationDate: { type: Date, required: true },
      business: { type: String, required: true },
      amount: { type: Number },
      batches: { type: String, required: true },
      flow: { type: Array },
      is_delete: { type: Boolean, default: false },
      createdAt: { type: Number },
      updatedAt: { type: Number },
    },
    {
      timestamps: true,
      toJSON: {
        transform(_doc, ret) {
          delete ret.__v;
        },
      },
    }
  );
  WarehouseScheme.plugin(AutoIncrement, {
    inc_field: "id",
    id: "Warehouse_counter",
  });
  return mongoose.model<WarehouseModelProps>("Warehouse", WarehouseScheme);
}

export default initWarehouseModel;
